package common

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/backend_controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	optionDto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common/option_dto"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/common"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type OptionController struct {
	backend_controller.BackendBaseController
	optionService common.OptionServiceIface
	commonService common.CommonServiceIface
}

func NewOptionController(optionService common.OptionServiceIface,
	commonService common.CommonServiceIface,
) *OptionController {
	return &OptionController{
		optionService: optionService,
		commonService: commonService,
	}
}

func (c *OptionController) RegisterRouter(g *gin.RouterGroup) {
	g.GET("/common/option/get-option-by-group", c.getOption)

	g.GET("/common/fuzzy-search-company-info", c.fuzzySearchCompanyInfo)
}

func (c *OptionController) getOption(ctx *gin.Context) {
	var request optionDto.GetOptionRequest
	if err := ctx.ShouldBind(&request); err != nil {
		stark.Logger.Errorf(ctx, "http getOption 参数错误：%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	result, err := c.optionService.GetOption(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, result)
}

// fuzzySearchCompanyInfo 模糊搜索企业工商信息（调用openapi接口）
func (c *OptionController) fuzzySearchCompanyInfo(ctx *gin.Context) {
	keyword := ctx.Query("keyword")
	if keyword == "" {
		c.Success(ctx, make([]map[string]interface{}, 0))
		return
	}

	data, err := c.commonService.FuzzySearchCompanyInfo(ctx, keyword)
	if err != nil {
		stark.Logger.Errorf(ctx.Request.Context(), "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, data)
}
